package regpang.myapplication22.statistical;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * 雷达统计图
 * ***************************************
 * author：琚涛
 * time：2017/3/22
 * description：https://www.jianshu.com/p/afe7bfe7a3ee
 * ****************************************
 */

public class RadarView extends View {

    //数据个数
    private int count = 5;
    //网格最大半径
    private float radius;
    //中心X
    private int centerX;
    //中心Y
    private int centerY;
    //雷达区画笔
    private Paint mainPaint;
    //文本画笔
    private Paint textPaint;
    //数据区画笔
    private Paint valuePaint;
    //标题文字
    private List<String> titles;
    //各维度分值
    private List<Double> data;
    //数据最大值
    private float maxValue = 100;
    private float angle;
    //绘制数据层数
    private int drawMore = 1;
    private List<ProjectBean> moreValues;

    public RadarView(Context context) {
        this(context, null);
    }

    public RadarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mainPaint = new Paint();
        mainPaint.setColor(0xFF00A8FF);
        mainPaint.setAntiAlias(true);
        mainPaint.setStrokeWidth(1);
        mainPaint.setStyle(Paint.Style.STROKE);

        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(20);
        textPaint.setStrokeWidth(1);
        textPaint.setAntiAlias(true);

        valuePaint = new Paint();
        valuePaint.setColor(0xFF00A8FF);
        valuePaint.setAntiAlias(true);
        valuePaint.setStyle(Paint.Style.FILL);

        titles = new ArrayList<>(count);
        titles.add("基础能力展示一");
        titles.add("基础能力展示二");
        titles.add("基础能力展示三");
        titles.add("基础能力展示四");
        titles.add("基础能力展示五");

        data = new ArrayList<>();
        data.add(60.0);
        data.add(60.0);
        data.add(60.0);
        data.add(60.0);
        data.add(60.0);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        radius = Math.min(w, h) / 2 * 0.6f;
        centerX = w / 2;
        centerY = h / 2;
        //一旦size发生改变，重新绘制
        postInvalidate();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawPolygon(canvas);
//        drawLines(canvas);//绘制对角线
        drawTitle(canvas);
        if (drawMore > 1) {
            //画多层
            for (int i = 0; i < drawMore; i++) {
                drawRegionMore(canvas, i);
            }
        } else {
            //画一层
            drawRegion(canvas);
        }
    }

    /**
     * 绘制多边形
     *
     * @param canvas
     */
    private void drawPolygon(Canvas canvas) {
        Path path = new Path();
        //1度=1*PI/180   360度=2*PI   那么我们每旋转一次的角度为2*PI/内角个数
        //中心与相邻两个内角相连的夹角角度
        angle = (float) (2 * Math.PI / count);
        //每个蛛丝之间的间距
        float r = radius / (count + 1);
        for (int i = 0; i < count + 2; i++) {
            //当前半径
            float curR = r * i;
            path.reset();
            for (int j = 0; j < count; j++) {
                if (j == 0) {
                    path.moveTo(centerX, centerY - curR);
                } else {
                    //对于直角三角形sin(x)是对边比斜边，cos(x)是底边比斜边，tan(x)是对边比底边
                    //因此可以推导出:底边(x坐标)=斜边(半径)*cos(夹角角度)
                    //               对边(y坐标)=斜边(半径)*sin(夹角角度)
//                    float x = (float) (centerX + curR * Math.cos(angle * j));
//                    float y = (float) (centerY - curR * Math.sin(angle * j));
                    float x = (float) (centerX + curR * Math.sin(angle * j));
                    float y = (float) (centerY - curR * Math.cos(angle * j));
                    path.lineTo(x, y);
                }
            }
            path.close();
            //最后一圈加粗
            if (i == count + 1) {
                mainPaint.setStrokeWidth(3);
            } else {
                mainPaint.setStrokeWidth(1);
            }
            canvas.drawPath(path, mainPaint);
        }
    }

    /**
     * 绘制直线
     */
    private void drawLines(Canvas canvas) {
        Path path = new Path();
        for (int i = 0; i < count; i++) {
            path.reset();
            path.moveTo(centerX, centerY);
            float x = (float) (centerX + radius * Math.cos(angle * i));
            float y = (float) (centerY + radius * Math.sin(angle * i));
            path.lineTo(x, y);
            canvas.drawPath(path, mainPaint);
        }
    }

    /**
     * 绘制标题文字
     *
     * @param canvas
     */
    private void drawTitle(Canvas canvas) {
        if (count != titles.size()) {
            return;
        }
        //相关知识点:http://mikewang.blog.51cto.com/3826268/871765/
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float fontHeight = fontMetrics.descent - fontMetrics.ascent;
        //绘制文字时不让文字和雷达图形交叉,加大绘制半径
        float textRadius = radius + fontHeight;
        double pi = Math.PI;
        for (int i = 0; i < count; i++) {
//            float x = (float) (centerX + textRadius * Math.cos(angle * i));
//            float y = (float) (centerY + textRadius * Math.sin(angle * i));
            float x = (float) (centerX + textRadius * Math.sin(angle * i));
            float y = (float) (centerY - textRadius * Math.cos(angle * i));
            //当前绘制标题所在顶点角度
            float degrees = angle * i;
            //从右下角开始顺时针画起,与真实坐标系相反
            if (degrees >= 0 && degrees < pi / 2) {//第四象限
                float dis = textPaint.measureText(titles.get(i)) / (titles.get(i).length() - 1);
                canvas.drawText(titles.get(i), x + dis, y + 10, textPaint);
            } else if (degrees >= pi / 2 && degrees < pi) {//第三象限
                float dis = textPaint.measureText(titles.get(i)) / (titles.get(i).length() - 1);
                canvas.drawText(titles.get(i), x + dis, y + 10, textPaint);
            } else if (degrees >= pi && degrees < 3 * pi / 2) {//第二象限
                float dis = textPaint.measureText(titles.get(i)) / (titles.get(i).length());
                canvas.drawText(titles.get(i), x - dis, y + 10, textPaint);
            } else if (degrees >= 3 * pi / 2 && degrees <= 2 * pi) {//第一象限
                canvas.drawText(titles.get(i), x, y, textPaint);
            }
        }

    }

    /**
     * 绘制覆盖区域
     */
    private void drawRegion(Canvas canvas) {
        valuePaint.setAlpha(255);
        Path path = new Path();
        for (int i = 0; i < count; i++) {
            //计算该数值与最大值比例
            Double perCenter = data.get(i) / maxValue;
            //小圆点所在位置距离圆心的距离
            double perRadius = perCenter * radius;
            float x = (float) (centerX + perRadius * Math.sin(angle * i));
            float y = (float) (centerY - perRadius * Math.cos(angle * i));
            if (i == 0) {
                path.moveTo(x, y);
            } else {
                path.lineTo(x, y);
            }
            //绘制小圆点
//            canvas.drawCircle(x, y, 10, valuePaint);
        }
        //闭合覆盖区域
        path.close();
        valuePaint.setStyle(Paint.Style.STROKE);
        //绘制覆盖区域外的连线
        canvas.drawPath(path, valuePaint);
        //填充覆盖区域，透明度设置
        valuePaint.setAlpha(128);
        valuePaint.setStyle(Paint.Style.FILL);
        canvas.drawPath(path, valuePaint);
    }

    /**
     * 绘制多层覆盖区域
     *
     * @param canvas
     * @param num 第几层
     */
    private void drawRegionMore(Canvas canvas, int num) {
        ProjectBean beanList = moreValues.get(num);

        try {
            valuePaint.setColor(Color.parseColor("#FF" + beanList.getColor()));
        } catch (Exception e) {
            e.printStackTrace();
//            LogX.e("####成绩单颜色格式不对");
        }
        valuePaint.setAlpha(255);
        Path path = new Path();
        for (int i = 0; i < count; i++) {
            //计算该数值与最大值比例
            Double perCenter = beanList.getValues().get(i) / maxValue;
//            Double perCenter = (1 + Math.random() * (100 - 1 + 1)) / maxValue;
            //小圆点所在位置距离圆心的距离
            double perRadius = perCenter * radius;
            float x = (float) (centerX + perRadius * Math.sin(angle * i));
            float y = (float) (centerY - perRadius * Math.cos(angle * i));
            if (i == 0) {
                path.moveTo(x, y);
            } else {
                path.lineTo(x, y);
            }
            if (perCenter == 0)
                //绘制小圆点
                canvas.drawCircle(x, y, 5, valuePaint);
        }
        //闭合覆盖区域
        path.close();
        valuePaint.setStyle(Paint.Style.STROKE);
        //绘制覆盖区域外的连线
        canvas.drawPath(path, valuePaint);
        //填充覆盖区域
        valuePaint.setAlpha(128);
        valuePaint.setStyle(Paint.Style.FILL);
        canvas.drawPath(path, valuePaint);
    }

    //设置数值种类
    public void setCount(int count) {
        this.count = count;
//        postInvalidate();
    }

    //设置蜘蛛网颜色
    public void setMainPaint(Paint mainPaint) {
        this.mainPaint = mainPaint;
        postInvalidate();
    }

    //设置标题颜色
    public void setTextPaint(Paint textPaint) {
        this.textPaint = textPaint;
    }

    //设置标题
    public void setTitles(ArrayList<String> titles) {
        this.titles = titles;
    }

    //设置覆盖局域颜色
    public void setValuePaint(Paint valuePaint) {
        this.valuePaint = valuePaint;
        postInvalidate();
    }

    //设置数值
    public void setData(List<Double> data) {
        this.data = data;
//        postInvalidate();
    }

    public List<Double> getData() {
        return data;
    }

    //设置最大数值
    public void setMaxValue(float maxValue) {
        this.maxValue = maxValue;
    }

    public void refreshData() {
        postInvalidate();
    }

    /**
     * 设置层数
     *
     * @param drawMore 层数
     * @param moreValues 具体数值
     */
    public void setDrawMore(int drawMore, List<ProjectBean> moreValues) {
        this.drawMore = drawMore;
        this.moreValues = moreValues;
    }

    public void setDrawMore(int drawMore) {
        this.drawMore = drawMore;
    }

    /**
     * //填充区域的层数数据，包括颜色和数值
     */
    public static class ProjectBean {
        private int color;//项目颜色
        private String name;//项目名称
        private List<Double> values;//数值信息

        public List<Double> getValues() {
            return values;
        }

        public void setValues(List<Double> values) {
            this.values = values;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

